package com.yichuang.wuziqi.activity;

import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.yichuang.wuziqi.R;
import com.yichuang.wuziqi.game.Game;
import com.yichuang.wuziqi.game.LocalGame;
import com.yichuang.wuziqi.player.LocalPlayer;
import com.yichuang.wuziqi.player.Playable;
import com.yichuang.wuziqi.player.Player;
import com.yichuang.wuziqi.tool.Tools;
import com.yichuang.wuziqi.view.BoardPanel;

public class CollectionActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "xcm";

    private Button startBtn;
    private Button endBtn;
    private Button saveBtn;
    private Button takeBackBtn;
    private BoardPanel boardPanel;
    private TextView roundCountTv;

    private int roundCount = 0;
    private int myTime;
    private int oppositeTime;

    private Player whitePlayer;
    private Player blackPlayer;

    private LocalGame currentGame;

    private int gameStatus = 0;

    private final int STATUS_INTI = 0;
    private final int STATUS_STARTED = 1;
    private final int STATUS_SAVED = 2;

    private final int MSG_DROP = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.data_colletion);
        initView();
    }

    private void initView() {
        startBtn = findViewById(R.id.start);
        endBtn = findViewById(R.id.end);
        saveBtn = findViewById(R.id.save);
        takeBackBtn = findViewById(R.id.take_back);
        roundCountTv = findViewById(R.id.round_count);

        startBtn.setOnClickListener(this);
        endBtn.setOnClickListener(this);
        endBtn.setEnabled(false);
        saveBtn.setOnClickListener(this);
        saveBtn.setEnabled(false);
        takeBackBtn.setOnClickListener(this);

        boardPanel = findViewById(R.id.panel);

        whitePlayer = new LocalPlayer(Playable.WHITE_SIDE, boardPanel);
        blackPlayer = new LocalPlayer(Playable.BLACK_SIDE, boardPanel);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start:
                handleGameStart();
                break;
            case R.id.end:
                gameStatus = STATUS_INTI;
                startBtn.setEnabled(true);
                endBtn.setEnabled(false);
                saveBtn.setEnabled(true);
                takeBackBtn.setEnabled(true);
                currentGame.setGameOver(true);
                break;
            case R.id.save:
                if (currentGame.save()) {
                    gameStatus = STATUS_SAVED;
                    currentGame.setGameOver(false);
                    startBtn.setEnabled(true);
                    saveBtn.setEnabled(false);
                } else {
                    Tools.makeToast(CollectionActivity.this, "保存游戏失败");
                }
                break;
            case R.id.take_back:
                currentGame.takeBack();
                if (--roundCount <= 0) {
                    takeBackBtn.setEnabled(false);
                }
                roundCountTv.setText("回合数：" + roundCount);
                break;
        }
    }

    private void handleGameStart() {
        boardPanel.reset();
        currentGame = new LocalGame(whitePlayer, blackPlayer);
        currentGame.setRecorder(boardPanel);

        currentGame.setGameListener(new Game.GameListener() {
            @Override
            public void onDrop(int who, Point position) {
                myHandler.sendEmptyMessage(MSG_DROP);
            }

            @Override
            public void onGameStatusChanged(int status) {
                gameStatusChanged(status);
            }
        });

        gameStatus = STATUS_STARTED;
        startBtn.setEnabled(false);
        endBtn.setEnabled(true);
        saveBtn.setEnabled(true);
        takeBackBtn.setEnabled(false);

        roundCount = 0;
        roundCountTv.setText("回合数：" + roundCount);

        new Thread(currentGame).start();
    }

    private void gameStatusChanged(int status) {
        switch (status) {
            case LocalGame.GAME_STATUS_END:

                break;
        }
    }

    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_DROP:
                    roundCount++;
                    if (roundCount > 0) {
                        takeBackBtn.setEnabled(true);
                    }
                    roundCountTv.setText("回合数：" + roundCount);
                    break;
            }
        }
    };

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        currentGame.setGameOver(true);
    }
}
